package com.information.utils;

import com.information.entity.User;

public class SensitiveDataUtil {
    
    /**
     * 脱敏邮箱地址
     * 例如: test@example.com -> t***@example.com
     */
    public static String maskEmail(String email) {
        if (email == null || email.isEmpty()) {
            return email;
        }
        
        int atIndex = email.indexOf('@');
        if (atIndex <= 1) {
            return email;
        }
        
        String username = email.substring(0, atIndex);
        String domain = email.substring(atIndex);
        
        if (username.length() <= 2) {
            return username.charAt(0) + "*" + domain;
        } else {
            return username.charAt(0) + "***" + domain;
        }
    }
    
    /**
     * 脱敏手机号码
     * 例如: 13812345678 -> 138****5678
     */
    public static String maskPhone(String phone) {
        if (phone == null || phone.isEmpty()) {
            return phone;
        }
        
        if (phone.length() < 7) {
            return phone;
        }
        
        if (phone.length() == 11) {
            // 中国手机号格式
            return phone.substring(0, 3) + "****" + phone.substring(7);
        } else {
            // 其他格式
            int visibleLength = Math.min(3, phone.length() / 3);
            String start = phone.substring(0, visibleLength);
            String end = phone.substring(phone.length() - visibleLength);
            return start + "****" + end;
        }
    }
    
    /**
     * 对用户对象进行敏感信息脱敏
     * @param user 原始用户对象
     * @param currentUserId 当前登录用户ID
     * @return 脱敏后的用户对象副本
     */
    public static User maskSensitiveData(User user, Long currentUserId) {
        if (user == null) {
            return null;
        }
        
        // 如果是用户自己，返回不脱敏的副本（但清除密码）
        if (currentUserId != null && currentUserId.equals(user.getId())) {
            User selfUser = new User();
            selfUser.setId(user.getId());
            selfUser.setUsername(user.getUsername());
            selfUser.setName(user.getName());
            selfUser.setEmail(user.getEmail()); // 不脱敏
            selfUser.setPhone(user.getPhone()); // 不脱敏
            selfUser.setAge(user.getAge());
            selfUser.setGender(user.getGender());
            selfUser.setAddress(user.getAddress());
            selfUser.setRole(user.getRole());
            selfUser.setIsDelete(user.getIsDelete());
            selfUser.setStatus(user.getStatus());
            selfUser.setCreatedAt(user.getCreatedAt());
            selfUser.setUpdatedAt(user.getUpdatedAt());
            selfUser.setPassword(null); // 密码永远不返回
            return selfUser;
        }
        
        // 创建用户对象副本进行脱敏
        User maskedUser = new User();
        maskedUser.setId(user.getId());
        maskedUser.setUsername(user.getUsername());
        maskedUser.setName(user.getName());
        maskedUser.setEmail(maskEmail(user.getEmail())); // 脱敏
        maskedUser.setPhone(maskPhone(user.getPhone())); // 脱敏
        maskedUser.setAge(user.getAge());
        maskedUser.setGender(user.getGender());
        maskedUser.setAddress(user.getAddress());
        maskedUser.setRole(user.getRole());
        maskedUser.setIsDelete(user.getIsDelete());
        maskedUser.setStatus(user.getStatus());
        maskedUser.setCreatedAt(user.getCreatedAt());
        maskedUser.setUpdatedAt(user.getUpdatedAt());
        maskedUser.setPassword(null); // 密码永远不返回
        
        return maskedUser;
    }

    /**
     * 对用户对象进行敏感信息脱敏（使用用户名比较）
     * @param user 原始用户对象
     * @param currentUsername 当前登录用户名
     * @return 脱敏后的用户对象副本
     */
    public static User maskSensitiveDataByUsername(User user, String currentUsername) {
        if (user == null) {
            return null;
        }
        
        // 如果是用户自己，返回不脱敏的副本（但清除密码）
        if (currentUsername != null && currentUsername.equals(user.getUsername())) {
            User selfUser = new User();
            selfUser.setId(user.getId());
            selfUser.setUsername(user.getUsername());
            selfUser.setName(user.getName());
            selfUser.setEmail(user.getEmail()); // 不脱敏
            selfUser.setPhone(user.getPhone()); // 不脱敏
            selfUser.setAge(user.getAge());
            selfUser.setGender(user.getGender());
            selfUser.setAddress(user.getAddress());
            selfUser.setRole(user.getRole());
            selfUser.setIsDelete(user.getIsDelete());
            selfUser.setStatus(user.getStatus());
            selfUser.setCreatedAt(user.getCreatedAt());
            selfUser.setUpdatedAt(user.getUpdatedAt());
            selfUser.setPassword(null); // 密码永远不返回
            return selfUser;
        }
        
        // 创建用户对象副本进行脱敏
        User maskedUser = new User();
        maskedUser.setId(user.getId());
        maskedUser.setUsername(user.getUsername());
        maskedUser.setName(user.getName());
        maskedUser.setEmail(maskEmail(user.getEmail())); // 脱敏
        maskedUser.setPhone(maskPhone(user.getPhone())); // 脱敏
        maskedUser.setAge(user.getAge());
        maskedUser.setGender(user.getGender());
        maskedUser.setAddress(user.getAddress());
        maskedUser.setRole(user.getRole());
        maskedUser.setIsDelete(user.getIsDelete());
        maskedUser.setStatus(user.getStatus());
        maskedUser.setCreatedAt(user.getCreatedAt());
        maskedUser.setUpdatedAt(user.getUpdatedAt());
        maskedUser.setPassword(null); // 密码永远不返回
        
        return maskedUser;
    }
    
    /**
     * 对用户列表进行敏感信息脱敏
     */
    public static java.util.List<User> maskSensitiveDataList(java.util.List<User> users, Long currentUserId) {
        if (users == null) {
            return null;
        }
        
        return users.stream()
                .map(user -> maskSensitiveData(user, currentUserId))
                .collect(java.util.stream.Collectors.toList());
    }
}
